package com.xls.dormitoryservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.dormitoryservice.entity.Visitor;
import com.xls.dormitoryservice.mapper.VisitorMapper;
import com.xls.dormitoryservice.service.VisitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xls.dormitoryservice.vo.VisitorQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor> implements VisitorService {

    @Autowired
    private VisitorMapper visitorMapper;

    @Override
    public boolean saveVisitor(Visitor visitor) {
        return this.save(visitor);
    }

    @Override
    public boolean updateVisitor(Visitor visitor) {
        return this.updateById(visitor);
    }

    @Override
    public Page<Visitor> getVisitorPage(Integer page, Integer limit, VisitorQuery visitorQuery) {
        QueryWrapper<Visitor> visitorQueryWrapper = new QueryWrapper<>();
        String building = visitorQuery.getBuilding();
        String name = visitorQuery.getName();
        String phone = visitorQuery.getPhone();
        String begin = visitorQuery.getBegin();
        String end = visitorQuery.getEnd();
        visitorQueryWrapper
                .eq(!StringUtils.isEmpty(building), "building", building)
                .likeRight(!StringUtils.isEmpty(name), "name", name)
                .likeRight(!StringUtils.isEmpty(phone), "phone", phone)
                .gt(!StringUtils.isEmpty(begin), "visiting_time", begin)
                .lt(!StringUtils.isEmpty(end), "visiting_time", end)
                .eq("is_deleted", 0);
        return visitorMapper.getVisitorPage(new Page<>(page, limit), visitorQueryWrapper);
    }

}
